import { createStore } from "vuex"
import createPersistedState from "vuex-persistedstate"

const store = createStore({
    plugins: [createPersistedState()],
    state: {
        collapsed: false,
        userinfo: {
            isLogin: false,
            token: null,
            info: null,
            menus: null,
        },
    },
    mutations: {
        collapsed(state) {
            state.collapsed = !state.collapsed
        },
        login(state, token) {
            state.userinfo = {
                isLogin: true,
                token: token,
                info: null,
                menus: null,
            }
        },
        logout(state) {
            state.userinfo = {
                isLogin: false,
                token: null,
                info: null,
                menus: null,
            }
        },
        updateInfo(state, info) {
            state.userinfo.info = info
        },
        updateMenus(state, menus) {
            state.userinfo.menus = menus
        },
    },
    actions: {},
})

if (window.innerWidth < 600) {
    if (!window.localStorage["vuex"]) {
        store.state.collapsed = true
    }
}

export { store }
